//
//  KPMessageViewController.swift
//  KPBaseProject
//
//  Created by 孑孓 on 2018/4/4.
//  Copyright © 2018年 Kipp. All rights reserved.
//

import UIKit
import TangramKit

class KPMessageViewController: KPBaseViewController{

    fileprivate var scrollView: UIScrollView!
    fileprivate var titleLa: UILabel!
    fileprivate var typeLa: UILabel!
    fileprivate var typeBtn = UIButton()
    fileprivate var contentLa: UILabel!
    fileprivate var contentTextView: UITextView!
    fileprivate var commitBtn: UIButton!
    fileprivate var dataArray = [String]()
    fileprivate var selectedArray = [String]()
    override func viewDidLoad() {
        super.viewDidLoad()
        createUI()

    }

    // MARK: 创建视图
    func createUI() {
        title = "意见反馈"

        dataArray = ["功能有问题?","图片加载不出来?","视频太卡?","费流量?","Test","功能有问题功能有问题?","1","哈哈","阿加西","孑孓不独活"]

        scrollView = UIScrollView.init(frame: CGRect.init(x: 0, y: 0, width: Screen.width, height: Screen.height - kNavibarH))
        scrollView.backgroundColor = UIColor.white
        scrollView.showsVerticalScrollIndicator = false
        view.addSubview(scrollView)

        let layout = TGRelativeLayout()
        layout.tg_cacheEstimatedRect = true
        layout.tg_top.equal(0)
        layout.tg_left.equal(0)
        layout.tg_width.equal(.fill)
        layout.tg_height.equal(.wrap)
        layout.backgroundColor = UIColor.white
        layout.tg_padding = UIEdgeInsets(top: 0, left: 0, bottom: 20, right: 0)
        scrollView.addSubview(layout)

        titleLa = UILabel.init(text: nil, font: 12.font, textColor: detailTextColor).then {
            $0.tg_left.equal(10)
            $0.tg_top.equal(15)
            $0.tg_width.equal(Screen.width - 20)
            $0.tg_height.equal(.wrap)
            layout.addSubview($0)
        }

        typeLa = UILabel.init(text: nil, font: 14.font, textColor: UIColor.black).then {
            $0.tg_left.equal(10)
            $0.tg_top.equal(titleLa.tg_bottom).offset(20)
            $0.tg_width.equal(Screen.width - 20)
            $0.tg_height.equal(.wrap)
            layout.addSubview($0)
        }

        let s = TGFlowLayout()
        s.tg_orientation = .vert
        s.tg_left.equal(0)
        s.tg_top.equal(typeLa.tg_bottom).offset(30)
        s.tg_height.equal(.wrap)
        s.tg_width.equal(.fill)
        s.tg_gravity = TGGravity.horz.fill
        s.tg_padding = UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 10)
        s.tg_space = 10

        for (i,data) in dataArray.enumerated() {
            typeBtn = UIButton.init(type: UIButtonType.custom)
            typeBtn.tg_height.equal(40)
            typeBtn.tg_width.equal(.wrap).add(10)
            typeBtn.setTitle(data, for: UIControlState.normal)
            typeBtn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
            typeBtn.layer.borderColor = UIColor.black.cgColor
            typeBtn.layer.borderWidth = 1
            typeBtn.backgroundColor = UIColor.white
            typeBtn.setTitleColor(UIColor.black, for: UIControlState.normal)
            typeBtn.setTitleColor(UIColor.blue, for: UIControlState.selected)
            typeBtn.tag = i + 1
            typeBtn.addTarget(self, action: #selector(typeBtnClick(btn:)), for: UIControlEvents.touchUpInside)
            s.addSubview(typeBtn)
        }
        layout.addSubview(s)
        s.backgroundColor = UIColor.white

        let aView = UIView()
        aView.tg_left.equal(0)
        aView.tg_top.equal(s.tg_bottom).offset(15)
        aView.tg_width.equal(.fill)
        aView.tg_height.equal(1)
        aView.backgroundColor = UIColor.gray
        layout.addSubview(aView)

        contentLa = UILabel.init(text: nil, font: 14.font, textColor: UIColor.black).then {
            $0.tg_left.equal(typeLa.tg_left)
            $0.tg_top.equal(aView.tg_bottom).offset(15)
            $0.tg_width.equal(.wrap)
            $0.tg_height.equal(.wrap)
            layout.addSubview($0)
        }

        contentTextView = UITextView.init().then {
            $0.tg_left.equal(titleLa.tg_left)
            $0.tg_top.equal(contentLa.tg_bottom).offset(35)
            $0.tg_width.equal(titleLa.tg_width)
            $0.tg_height.equal(200)
            $0.backgroundColor = UIColor.gray
            $0.layer.cornerRadius = 5
            $0.clipsToBounds = true
            layout.addSubview($0)
        }

        let bView = UIView()
        bView.tg_left.equal(0)
        bView.tg_top.equal(contentTextView.tg_bottom).offset(10)
        bView.tg_width.equal(.fill)
        bView.tg_height.equal(1)
        bView.backgroundColor = UIColor.gray
        layout.addSubview(bView)

        commitBtn = UIButton.init().then {
            $0.tg_left.equal(20)
            $0.tg_top.equal(bView.tg_bottom).offset(15)
            $0.tg_width.equal(Screen.width - 40)
            $0.tg_height.equal(40)
            $0.setTitle("提交", for: UIControlState.normal)
            $0.setTitleColor(UIColor.white, for: UIControlState.normal)
            $0.titleLabel?.font = UIFont.systemFont(ofSize: 14)
            $0.backgroundColor = UIColor.blue
            $0.layer.cornerRadius = 5
            $0.clipsToBounds = true
            $0.addTarget(self, action: #selector(commitBtnClick(btn:)), for: UIControlEvents.touchUpInside)
            layout.addSubview($0)
        }
        titleLa.text = "致力为您提供专业、搞笑、高品质的学习视频直播，欢迎您提供宝贵的意见和反馈"
        typeLa.text = "反馈类型"
        contentLa.text = "反馈内容"

        scrollView.contentSize = CGSize.init(width: 0, height: layout.frame.size.height)
    }

    // MARK: Action
    @objc func commitBtnClick(btn : UIButton) {
        if selectedArray.isEmpty {
            showAlertView(title: "提示", content: "请最少选择一个反馈类型")
        } else if contentTextView.text.isEmpty {
            showAlertView(title: "提示", content: "请输入反馈内容")
        } else {
            commitProblem(type: selectedArray.joined(separator: ","), content: contentTextView.text!)
            KPLog(contentTextView.text!)
        }
    }

    @objc func typeBtnClick(btn : UIButton) {
        btn.isSelected = !btn.isSelected
        if btn.isSelected {
            selectedArray.append(dataArray[btn.tag - 1])
            KPLog(selectedArray)
        } else {
            let unSelected = dataArray[btn.tag - 1]
            selectedArray.removeAll(unSelected)
            KPLog(selectedArray)
        }
    }

    // MARK: NetworkRequest
    fileprivate func commitProblem(type: String, content:String) {
//        showMessageLoading(message: "问题反馈中，请稍后...")
        Api.postJSONRequest(ApiType.添加反馈, parameters: ["title":type, "content":content])
        .mapBaseModel()
            .subscribe(onNext: { (model) in
                KPLog("返回状态码：\(model.resultState)---返回请求结果：\(model.msg)---返回数据\(String(describing: model.data))")
                if model.resultState == 1{
                    self.showAlertView(title: "提示", content: "提交成功")
                }else{
                    self.showAlertView(title: "提示", content: model.msg)
                }
//                self.hideLoading()
            }, onError: { (error) in
                KPLog("错误---\(error)")
                self.showAlertView(title: "提示", content: "提交失败")
//                self.hideLoading()
            })
            .disposed(by: bag)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}
